package com.tensquare.rest;


import com.tensquare.client.LabelClient;
import com.tensquare.entity.Problem;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 问答
 */
@RestController
@RequestMapping("problem")
@CrossOrigin
public class ProblemRest {

    @Autowired
    private  ProblemService problemService;
    @Autowired
    private LabelClient labelClient;

    /**
     * 根据标签ID查询最新回复的问题列表
     * http://localhost:9003/problem/newlist/1/0/10
     * @param labelId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "/newlist/{labelId}/{page}/{limit}")
    public Result newlist(@PathVariable("labelId") String labelId, @PathVariable("page") int page, @PathVariable("limit") int limit){

        return new Result(true, StatusCode.OK, "查询成功", problemService.findNewListByLabelId(labelId, page, limit));
    }

    /**
     * 根据标签id查询热门问题列表
     * http://localhost:9003/problem/hotlist/1/0/10
     * @param labelId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "/hotlist/{labelId}/{page}/{limit}")
    public Result hotlist(@PathVariable("labelId") String labelId, @PathVariable("page") int page, @PathVariable("limit") int limit){

        return new Result(true, StatusCode.OK, "查询成功", problemService.findHotListByLabelId(labelId, page, limit));
    }

    /**
     * 根据标签id查询待回复问题列表
     * http://localhost:9003/problem/waitlist/1/0/10
     * @param labelId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "/waitlist/{labelId}/{page}/{limit}")
    public Result waitlist(@PathVariable("labelId") String labelId, @PathVariable("page") int page, @PathVariable("limit") int limit){

        return new Result(true, StatusCode.OK, "查询成功", problemService.findWaitListByLabelId(labelId, page, limit));
    }


    /**
     * 根据id查找标签
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable("id") String id){
        System.err.println("No.1  调用了服务");
        return new Result(true, StatusCode.OK, "查询成功", labelClient.findById(id));
    }

    /**
     * 发布问题
     * http://localhost:9003/problem/add
     * @param map
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Result add( Map<String, String> map){
        map.put("title", "测试服务通信？");
        map.put("content", "测试服务器之间的通信与调用是否正常");
        map.put("uid", "12344");
        boolean result = problemService.add(map);
        if (result)
            return new Result(true, StatusCode.OK, "发布成功");
        else
            return new Result(false, StatusCode.ERROR, "发布失败");
    }
}
